/**
 * Created by Administrator on 2017/6/6.
 */
var carInitialsChooseBoss;
$(function(){
    "use strict";
    //自由交换站台事件
    $('.changeSeat').on('tap',function(){
        var startSeat=$('.startSeat').text(),endSeat=$('.endSeat').text();
        var a=startSeat,b=endSeat;
        setTimeout(function(){
            $('.startSeat').text(b);
            $('.endSeat').text(a);
        },50);
    });

//汽车开头选择按钮
    $('.carInitialsUl>li>div').on('tap',function(){
          var hehe=$(this).attr("hehe"),xixi=$(this).attr("xixi");
        $('.carInitialsUl>li>div').attr("hehe","fasle");
        $('.carInitialsUl>li>div').css({backgroundColor:"#F8F8F8",color:"black"});
        $(this).attr("hehe","true");
        $(this).css({backgroundColor:"#18B4ED",color:"white"});
        carInitialsChooseBoss=xixi;

    });

    //跳转车站选择页面
    $('.station').on('tap',function(){
        var xixi=$(this).attr("xixi");
        var start=$('.startSeat').text();
        var end=$('.endSeat').text();
        localStorage.setItem("startOrEnd",xixi);
        if(localStorage.startOrEnd=="135"){
            localStorage.setItem("endVal",end);
        }else if(localStorage.startOrEnd=="246"){
            localStorage.setItem("startVal",start);
        }else{
            return false;
        }
        location.href="stationSearch.html";

    });


    //页面加载刷新出发站台或者目的站台
    function mYStationChoose(){
        if(localStorage.startOrEnd==""||localStorage.startOrEnd==null||localStorage.startOrEnd==undefined){
            setTimeout(function(){
                $('.startSeat').text("西昌");
                $('.endSeat').text("成都");
            },50)
        }else if(localStorage.startOrEnd=="135"){
            if(localStorage.myChoose==""||localStorage.myChoose==null||localStorage.myChoose==undefined){
                return false;
            }
            setTimeout(function(){
                $('.startSeat').text(localStorage.myChoose);
                $('.endSeat').text(localStorage.endVal);
            },50)
        }else if(localStorage.startOrEnd=="246"){
            if(localStorage.myChoose==""||localStorage.myChoose==null||localStorage.myChoose==undefined){
                return false;
            }
            setTimeout(function(){
                $('.startSeat').text(localStorage.startVal);
                $('.endSeat').text(localStorage.myChoose);
            },50)
        }else{
            return false;
        }
    }
    mYStationChoose();

    //取当前日期数拆分拼接成YYYY-MM-dd样式的函数
    function  nowDate(){
        var now_date=new Date();
        var year=now_date.getFullYear(),month=now_date.getMonth()+ 1,date=now_date.getDate();
        if(month<10){
            month="0"+month;
        }
        if(date<10){
            date="0"+date;
        }
        var newDate=year+"-"+month+"-"+date;
        return newDate;
    }



    //页面刷新加载最近常用路线
    function recRoute(){
        var user=localStorage.nameQQ;
        $.ajax({
            type:"post",
            url:"/recRouteShow",
            data:{user:user},
            success:function(date){
                if(date=="error"){
                    return false;
                }
                var html="";
                for(var i=0;i<date.length;i++){
                    html+='<li class="ui-border-b">'+date[i].startName+"--"+date[i].endName+'</li>';
                }
               setTimeout(function(){
                   $('.latelyRouteUl').html(html);
               },100)
            }
        });
    }
    recRoute();

    //点击查询添加最近常用路线的函数
    function addRecRoute(){
        setTimeout(function(){
            var user=localStorage.nameQQ;
            var start=$('.startSeat').text();
            var end=$('.endSeat').text();
            var date=nowDate();
            var obj={
                user:user,
                start:start,
                end:end,
                date:date
            };
            $.ajax({
                type:"post",
                url:"/addRecRoute",
                data:obj,
                success:function(date){

                }
            });
        },80)
    }

    //获得正确的出发日期的函数
    function getRightDate(Y,M,D){
        var myYear=document.getElementById(Y);
        var index=myYear.selectedIndex;
        var yearVal=myYear.options[index].text;
        var myMonth=document.getElementById(M);
        var index2=myMonth.selectedIndex;
        var monthVal=myMonth.options[index2].text;
        var myDay=document.getElementById(D);
        var index3=myDay.selectedIndex;
        var dayVal=myDay.options[index3].text;
        var RightDate=yearVal+"-"+monthVal+"-"+dayVal;
        return RightDate;
    }

    //查询车票
    function lookupTicket(){
        $('.checkDiv>button').on('tap',function(){
            var start=$('.startSeat').text()+"%";
            var end=$('.endSeat').text()+"%";
            var godate=getRightDate("myYear","myMonth","myDay");
            var goTime=document.getElementById("goTime");
            var index=goTime.selectedIndex;
            var goTimeSelected=goTime.options[index].text;
            var arry=goTimeSelected.split("--");
            var goTimeFirst=(arry[0].split(":"))[0],goTimeLast=(arry[1].split(":"))[0];
            var carInitialsChoose=carInitialsChooseBoss;
            if(carInitialsChoose==undefined||carInitialsChoose==null){
                $('#tipSection').text("请选择汽车趟次开头字母");
                $('.ui-poptips').show();
                return false;
            }
            var obj={
                start:start,
                end:end,
                godate:godate,
                goTimeFirst:goTimeFirst,
                goTimeLast:goTimeLast,
                carInitialsChoose:carInitialsChoose
            };

            //保存最近常用的AJAX函数
            function hehe(dom){
                var myDate=nowDate();
                var myUser=localStorage.nameQQ;
                var myobj={
                    seat:dom,
                    date:myDate,
                    user:myUser
                };
                $.ajax({
                    type:"post",
                    url:"/addMyRecntly",
                    data:myobj,
                    success:function(date){

                    }
                })
            }
            $.ajax({
                type:"post",
                url:"/lookupTicket",
                data:obj,
                success:function(date){
                    if(date=="error"){
                        $('#tipSection').text("无匹配以上信息车次");
                        $('.ui-poptips').show();
                        return false;
                    }else{
                        localStorage.setItem("StartLgy",$('.startSeat').text());
                        localStorage.setItem("endLgy",$('.endSeat').text());
                        localStorage.setItem("godateLgy",godate);
                        localStorage.setItem("goTimeFirstLgy",goTimeFirst);
                        localStorage.setItem("goTimeLastLgy",goTimeLast);
                        localStorage.setItem("carInitialsChooseLgy",carInitialsChoose);
                        addRecRoute();
                        var startxx=$('.startSeat').text();
                        var endxx=$('.endSeat').text();
                        hehe(startxx);
                        setTimeout(function(){
                            hehe(endxx);
                        },50)
                        setTimeout(function(){
                            location.href="./ticketInformation.html";
                        },100)

                    }
                }
            });
        });
    }
    lookupTicket();

    //点击确定关闭提示框
    function closeTip(){
        $('.continueLookUp').on('tap',function(){
            $('.closeTip').hide();
        })
    }
    closeTip();

    //点击最近常用路线中的一栏，修改路线
    function modifyRoute(){
        $(document).on('tap','.latelyRouteUl>li',function(){
           var text=$(this).text();
            var arry=text.split("--");
            var arryStart=arry[0],arryEnd=arry[1];
            $('.startSeat').text(arryStart);
            $('.endSeat').text(arryEnd);
        })
    }
    modifyRoute();


});